<?php

namespace App\Services\GameTool;

use App\Models\Config\Channels;
use App\Models\Config\Games;
use App\Models\Enum\MailStatus;
use App\Models\Enum\MailType;
use App\Repositories\Config\GamesRepository;
use App\Repositories\Config\ChannelsRepository;
use Auth;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

class UserInfoService
{
    public function __construct(GamesRepository $gamesRepository, ChannelsRepository $channelsRepository)
    {
        $this->gamesRepository = $gamesRepository;
        $this->channelsRepository = $channelsRepository;
    }

    //请求 用户信息列表
    public function getUserInfoList($request)
    {
        if (empty($request->channel_id) || empty($request->server_id)) {
            return [];
        }

        $game = $this->gamesRepository->where('game_id', session('game_id'))->first();
        $channel = $this->channelsRepository->getByChannelId($request->channel_id);

        $model = 'App\\Games\\' . $game->model_name;

        if (!class_exists($model)) {
            flash('游戏类名不存在')->error()->important();
            return false;
        }

        $class = new $model();
        $result = $class->getUserInfoList($request, $channel);

        if (isset($result['code']) && $result['code'] == 1) {
            $currentPage = $result['page_index'];
            //实例化collect方法
            $collection = new Collection($result['data']);
            //定义一下每页显示多少个数据
            $perPage = $result['page_size'];
            //获取当前需要显示的数据列表
            //$currentPageSearchResults = $collection->slice($currentPage * $perPage, $perPage)->all();
            //创建一个新的分页方法
            $paginatedSearchResults = new LengthAwarePaginator($collection, $result['total_count'], $perPage);
            $paginatedSearchResults->setPath(route('userinfo.index'));
            //$paginatedSearchResults->setPageName('page_index');

            return $paginatedSearchResults;
        } else {
            return [];
        }
    }

    //请求 用户信息详情
    public function getUserInfoDetail($request)
    {
        if (empty($request->channel_id)) {
            return [];
        }

        $game = $this->gamesRepository->where('game_id', session('game_id'))->first();
        $channel = $this->channelsRepository->getByChannelId($request->channel_id);

        $model = 'App\\Games\\' . $game->model_name;

        if (!class_exists($model)) {
            flash('游戏类名不存在')->error()->important();
            return false;
        }

        $class = new $model();
        return $class->getUserInfoDetail($request, $channel);
    }
}